package 岛屿问题;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author: AirMan
 * @date: 2025/5/29 9:53
 * @description:
 */
public class Solution2 {

    public final int[][] dir = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}}; // 表示四个方向

    public void bfs(int[][] graph, boolean[][] visited, int x, int y) {
        Deque<int[]> queue = new LinkedList<>();

        queue.push(new int[]{x, y});
        visited[x][y] = true; // 只要加入队列，立刻标记为访问过的节点
        while (!queue.isEmpty()) {
            int[] node = queue.pop();
            int curx = node[0];
            int cury = node[1];
            // 沿四个方向进行遍历
            for (int i = 0; i < 4; i++) {
                int nextx = curx + dir[i][0];
                int nexty = cury + dir[i][1];
                if (nextx < 0 || nextx >= graph.length || nexty < 0 || nexty >= graph.length) continue; // 坐标越界
                if (!visited[nextx][nexty]) {
                    queue.push(new int[]{nextx, nexty});
                    visited[nextx][nexty] = true;
                }
            }
        }
    }
}
